package com.cy.controller;


import com.cy.bfzcommons.pojo.Picture;
import com.cy.bfzcommons.service.PictureService;
import com.cy.bfzcommons.web.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/picture")
@RestController
@CrossOrigin
@Api(
        value = "/picture",description = "PictureController"
        ,tags = "商品相关"
)
public class PictureController {
    @Autowired
    private PictureService pictureService;





    /**
     * 修改商品图片
     * @param picture  通过FileController删除原有的图片地址  再存储新的地址
     * @return
     */

    @PutMapping
    public JsonResult updatePicture(@ApiParam(name="picture",value="商品图片信息",required=true)@RequestBody Picture picture){
         pictureService.updatePicture(picture);
        return JsonResult.ok();
    }

    /**
     * 删除商品图片  先调用FileController的图片地址删除本地图片 再删除数据库的地址
     * @param pictureId
     * @return
     */

    @DeleteMapping("{pictureId}")
    public JsonResult deletePicture(@ApiParam(name="picture",value="商品图片信息",required=true)@PathVariable Integer pictureId){
        pictureService.deletePicture(pictureId);
        return JsonResult.ok();
    }

    /**
     * 新增商品图片  先调用FileController的新增方法  返回一个图片磁盘地址赋值给picture对象
     * 再进行新增操作   需要在上传图片的时候执行FileController的新增方法  在表单提交的时候
     * 执行该方法
     * @param picture
     * @return
     */

    @PostMapping
    public JsonResult savePicture(@ApiParam(name="picture",value="商品图片信息",required=true)@RequestBody Picture picture){
        pictureService.savePicture(picture);
        return JsonResult.ok();
    }

    /**
     * 根据id查询图片
     * @param pictureId
     * @return
     */

    @GetMapping("{pictureId}")
    public JsonResult getPicture(@ApiParam(name="pictureId",value="商品图片id",required=true)@PathVariable Integer pictureId){
        Picture picture = pictureService.getPicture(pictureId);
        return JsonResult.ok(picture);
    }
}
